package hanxx.security.dao;


import hanxx.system.dao.BaseDao;
import hanxx.security.domain.SysUserRole;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.io.Serializable;

/**
 * 用户与角色关联表 数据层
 *
 */
public interface SysUserRoleDao extends BaseDao<SysUserRole, Serializable> {
    /**
     * 通过用户ID删除用户和角色关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Modifying
    @Query(value = "delete from sys_user_role where user_id=?1", nativeQuery = true)
    public int deleteUserRoleByUserId(Long userId);

    /**
     * 通过用户ID删除用户和角色关联的单位角色
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Modifying
    @Query(value = "delete from sys_user_role where user_id=?1 and role_id in ('5','6','7','8')", nativeQuery = true)
    public int deleteCompanyUserRoleByUserId(Long userId);

    /**
     * 批量删除用户和角色关联
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Query(value = "delete from sys_user_role where user_id in (?1)", nativeQuery = true)
    @Modifying
    public int deleteUserRole(Long [] ids);

    /**
     * 通过角色ID查询角色使用数量
     *
     * @param roleId 角色ID
     * @return 结果
     */
    @Query(value = " select count(1) from sys_user_role where role_id=?1 ", nativeQuery = true)
    public int countUserRoleByRoleId(Long roleId);


    /**
     * 删除用户和角色关联信息
     *
     * @param userRole 用户和角色关联信息
     * @return 结果
     */
    @Modifying
    @Query(value = "delete from sys_user_role where user_id=:#{#userRole.userId} and role_id=:#{#userRole.roleId}", nativeQuery = true)
    public int deleteUserRoleInfo(@Param("userRole") SysUserRole userRole);

    /**
     * 批量取消授权用户角色
     *
     * @param roleId  角色ID
     * @param userIds 需要删除的用户数据ID
     * @return 结果
     */
    @Query(value = "delete from sys_user_role where role_id=?1 and user_id in (?2)", nativeQuery = true)
    @Modifying
    public int deleteUserRoleInfos(Long roleId, Long[] userIds);
}
